/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controllers;

import concursos.concurso.Ponto;
import controllers.exceptions.NonexistentEntityException;
import controllers.exceptions.PreexistingEntityException;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import concursos.concurso.NormaComplementar;

/**
 *
 * @author lucena
 */
public class PontoJpaController {

    public PontoJpaController() {
        emf = Persistence.createEntityManagerFactory("concursosPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Ponto ponto) throws PreexistingEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            NormaComplementar normaComplementar = ponto.getNormaComplementar();
            if (normaComplementar != null) {
                normaComplementar = em.getReference(normaComplementar.getClass(), normaComplementar.getId());
                ponto.setNormaComplementar(normaComplementar);
            }
            em.persist(ponto);
            if (normaComplementar != null) {
                normaComplementar.getPonto().add(ponto);
                normaComplementar = em.merge(normaComplementar);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findPonto(ponto.getId()) != null) {
                throw new PreexistingEntityException("Ponto " + ponto + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Ponto ponto) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Ponto persistentPonto = em.find(Ponto.class, ponto.getId());
            NormaComplementar normaComplementarOld = persistentPonto.getNormaComplementar();
            NormaComplementar normaComplementarNew = ponto.getNormaComplementar();
            if (normaComplementarNew != null) {
                normaComplementarNew = em.getReference(normaComplementarNew.getClass(), normaComplementarNew.getId());
                ponto.setNormaComplementar(normaComplementarNew);
            }
            ponto = em.merge(ponto);
            if (normaComplementarOld != null && !normaComplementarOld.equals(normaComplementarNew)) {
                normaComplementarOld.getPonto().remove(ponto);
                normaComplementarOld = em.merge(normaComplementarOld);
            }
            if (normaComplementarNew != null && !normaComplementarNew.equals(normaComplementarOld)) {
                normaComplementarNew.getPonto().add(ponto);
                normaComplementarNew = em.merge(normaComplementarNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                long id = ponto.getId();
                if (findPonto(id) == null) {
                    throw new NonexistentEntityException("The ponto with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(long id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Ponto ponto;
            try {
                ponto = em.getReference(Ponto.class, id);
                ponto.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The ponto with id " + id + " no longer exists.", enfe);
            }
            NormaComplementar normaComplementar = ponto.getNormaComplementar();
            if (normaComplementar != null) {
                normaComplementar.getPonto().remove(ponto);
                normaComplementar = em.merge(normaComplementar);
            }
            em.remove(ponto);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Ponto> findPontoEntities() {
        return findPontoEntities(true, -1, -1);
    }

    public List<Ponto> findPontoEntities(int maxResults, int firstResult) {
        return findPontoEntities(false, maxResults, firstResult);
    }

    private List<Ponto> findPontoEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Ponto as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Ponto findPonto(long id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Ponto.class, id);
        } finally {
            em.close();
        }
    }

    public int getPontoCount() {
        EntityManager em = getEntityManager();
        try {
            return ((Long) em.createQuery("select count(o) from Ponto as o").getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
